import { remove, getList, save } from '@/api/cust/car'
import { getSelects } from '@/api/house/community'
import { getCustSelects } from '@/api/cust/custhouseref'
import { getOptions } from '@/api/option'
import {isCarNo, isIntegerBig} from "../../../utils/validate";

export default {
  data() {
    return {
      formVisible: false,
      formTitle: '添加车辆信息',
      isAdd: true,
      form: {
        custId:'',
        communityId:'',
        carNo:'',
        vin:'',
        registerDate:'',
        state:'',
        id: '',
        createBy:'',
        createTime:'',
        electricFlag:'',
        carBrand:'',
        carModel:'',
        carNo1:'',
        carNo2:'',
        carNo3:''
      },
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined,
        carNo: undefined,
        communityId: undefined,
        custId: undefined
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      options:{
        state:[],
        electricFlag:[],
        communityIds:[],
        custIds:[],
        custIdsCopy:[],
        searchCustIds:[],
        searchCustIdsCopy:[],
        // area1:[ProvinceArea'吉','黑','辽','冀','鲁','豫','京','津','沪','渝','云','湘','皖','新','苏','浙','赣','鄂','桂','甘','晋','蒙','陕','闽','贵','粤','青','川','宁','琼'],
        // area2:['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
        area1:[],
        area2:[]
      },
      value:''
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  computed: {

    //表单验证
    rules() {
      return {
        communityId: [
          { required: true, message: '请选择社区', trigger: 'blur' }
        ],
        custId: [
          { required: true, message: '请选择客户', trigger: 'blur' }
        ],
        registerDate: [
          { required: true, message: '注册日期不能为空', trigger: 'blur' }
        ],
        electricFlag: [
          { required: true, message: '请选择是否新能源', trigger: 'blur' }
        ],
        // carNo:[
        //   {
        //     validator (rule, value, callback) {
        //       if(this.form.carNo1 === '' || this.form.carNo2 === '' || !isCarNo(this.form.carNo3)){
        //         callback()
        //       } else {
        //         callback(new Error('车牌号不合法'))
        //       }
        //     },
        //     trigger: 'blur'
        //   }
        // ]
      }
    }
  },
  created() {
    this.init()
    this.initOptions()
    this.initCommunitys()
  },
  methods: {
    init() {
      this.fetchData()
    },
    initOptions(){
      getOptions('State_YesOrNo_ProvinceShortName_ProvinceArea').then(response => {
        this.options.state = response.data.maps.State;
        this.options.electricFlag = response.data.maps.YesOrNo;
        this.options.area1 = response.data.maps.ProvinceShortName;
        this.options.area2 = response.data.maps.ProvinceArea;
      })
    },
    initCommunitys(){
      getSelects().then(response => {
        this.options.communityIds = response.data;
      })
    },
    getCusts(){
      this.form.custId = '';
      getCustSelects(this.form.communityId).then(response => {
        this.options.custIds = response.data;
        this.options.custIdsCopy = response.data;
      })
    },
    custFilter(val) {
      this.value = val;
      if (val) {
        this.options.custIds = this.options.custIdsCopy.filter((item) => {
          if (!!~item.custName.indexOf(val) || !!~item.custName.toUpperCase().indexOf(val.toUpperCase())) {
            return true
          }
        })
      } else {
        this.options.custIds = this.options.custIdsCopy;
      }
    },
    getCustsSearch(){
      this.listQuery.custId = '';
      getCustSelects(this.listQuery.communityId).then(response => {
        this.options.searchCustIds = response.data;
        this.options.searchCustIdsCopy = response.data;
      })
    },
    custFilterSearch(val) {
      this.value = val;
      if (val) {
        this.options.searchCustIds = this.options.searchCustIdsCopy.filter((item) => {
          if (!!~item.custName.indexOf(val) || !!~item.custName.toUpperCase().indexOf(val.toUpperCase())) {
            return true
          }
        })
      } else {
        this.options.searchCustIds = this.options.searchCustIdsCopy;
      }
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },
    search() {
      this.fetchData()
    },
    reset() {
      this.listQuery.id = ''
      this.listQuery.carNo = ''
      this.listQuery.communityId = ''
      this.listQuery.custId = ''
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClose() {

    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    resetForm() {
      this.form = {
        custId:'',
        communityId:'',
        carNo:'',
        vin:'',
        registerDate:'',
        state:1,
        id: '',
        electricFlag:'',
        carBrand:'',
        carModel:'',
        carNo1:'',
        carNo2:'',
        carNo3:''
      }
    },
    add() {
      this.resetForm()
      this.formTitle = '添加车辆信息',
      this.formVisible = true
      this.isAdd = true
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if(this.form.carNo1 === '' || this.form.carNo2 === '' || !isCarNo(this.form.carNo3)){
            this.$message({
              message: '车牌号不合法',
              type: 'error'
            })
            return false
          }else{
            save({
              custId:this.form.custId,
              communityId:this.form.communityId,
              carNo:(this.form.carNo1 + this.form.carNo2 + '·' + this.form.carNo3).toUpperCase(),
              vin:this.form.vin,
              registerDate:this.form.registerDate,
              state:this.form.state,
              id: this.form.id,
              createBy: this.form.createBy,
              createTime: this.form.createTime,
              electricFlag: this.form.electricFlag,
              carBrand: this.form.carBrand,
              carModel: this.form.carModel
            }).then(response => {
              this.$message({
                message: this.$t('common.optionSuccess'),
                type: 'success'
              })
              this.fetchData()
              this.formVisible = false
            })
          }
        } else {
          return false
        }
      })
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    edit() {
      if (this.checkSel()) {
        this.isAdd = false
        this.form = this.selRow
        this.formTitle = '编辑车辆信息'
        this.formVisible = true

        if(this.selRow.carNo){
          this.selRow.carNo1 = this.selRow.carNo.substring(0,1)
          this.selRow.carNo2 = this.selRow.carNo.substring(1,2)
          this.selRow.carNo3 = this.selRow.carNo.substring(3)
        }
        getCustSelects(this.selRow.communityId).then(response => {
          this.options.custIds = response.data;
          this.options.custIdsCopy = response.data;
        })
      }
    },
    remove() {
      if (this.checkSel()) {
        var id = this.selRow.id
        this.$confirm(this.$t('common.deleteConfirm'), this.$t('common.tooltip'), {
          confirmButtonText: this.$t('button.submit'),
          cancelButtonText: this.$t('button.cancel'),
          type: 'warning'
        }).then(() => {
          remove(id).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
          }).catch( err=> {
            this.$notify.error({
              title: '错误',
              message: err
            })
          })
        }).catch(() => {
        })
      }
    },
    change(e){
      this.$forceUpdate()
    }
  }
}
